package com.neegix.system.user.infrastructure.repository.impl;

import com.neegix.application.query.NebulaSQL;
import com.neegix.base.PageVO;
import com.neegix.system.user.application.assembler.UserAssembler;
import com.neegix.system.user.application.cqrs.query.UserQueryRepository;
import com.neegix.system.user.application.cqrs.query.condition.UserWhereGroup;
import com.neegix.system.user.application.dto.UserDTO;
import com.neegix.system.user.interfaces.vo.UserVO;
import com.neegix.system.user.infrastructure.repository.convert.UserConverter;
import com.neegix.system.user.infrastructure.repository.dataobject.UserDO;
import com.neegix.system.user.infrastructure.repository.mapper.UserMapper;
import com.neegix.system.user.infrastructure.repository.mapper.customized.UserCustomizedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-12-03 11:47:28
  */

@Repository
public class UserQueryRepositoryImpl implements UserQueryRepository {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserCustomizedMapper userCustomizedMapper;

    @Override
    public Optional<UserVO> findById(Long id) {
        UserDO userDO = userMapper.selectByPrimaryKey(id);
        return Optional.ofNullable(UserAssembler.INSTANCE.covertVO(UserConverter.INSTANCE.covertDTO(userDO)));
    }

    @Override
    public Integer selectCount(List<Long> ids) {
        return userCustomizedMapper.selectCountByIds(ids);
    }

    @Override
    public PageVO<UserVO> findPage(Integer currentPage, Integer pageSize, UserDTO userDTO) {
        NebulaSQL nebulaSQL = new NebulaSQL();
        nebulaSQL.createWhereGroups(UserWhereGroup.class)
           .andIdEqualTo(userDTO.getId())
           .andCreateTimeBetween(userDTO.getStartCreateTime(),userDTO.getEndCreateTime())
           .andUpdateTimeBetween(userDTO.getStartUpdateTime(),userDTO.getEndUpdateTime())
           .andCreateUserEqualTo(userDTO.getCreateUser())
           .andUpdateUserEqualTo(userDTO.getUpdateUser())
           .andNameLikeTo(userDTO.getName())
           .andPasswordLikeTo(userDTO.getPassword())
           .andDescriptionLikeTo(userDTO.getDescription())
           .andEmailLikeTo(userDTO.getEmail())
           .andMobilePhoneLikeTo(userDTO.getMobilePhone())
           .andEnabledEqualTo(userDTO.getEnabled())
           .andDeletedEqualTo(userDTO.getDeleted());
           
        nebulaSQL.setPager(currentPage, pageSize);
        List<UserDO> result = userMapper.selectList(nebulaSQL);
        Long total = userMapper.selectCount(nebulaSQL);
        PageVO<UserVO> page = new PageVO<>(currentPage, pageSize);
        page.setTotal(total);
        page.setResult(UserAssembler.INSTANCE.covertVO(UserConverter.INSTANCE.covertDTOS(result)));
        return page;
    }

    @Override
    public Set<Long> getRolesByPkUser(Long pkUser) {
        return userCustomizedMapper.selectRolesByPkUser(pkUser);
    }
}
